﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.Common;
using Module.Common.Models;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.Products;
//using Module.Core.BusinessLogic.ShopingCart;

using Module.Core.EntitiesInfo;
using Module.Core.EntitiesInfo.TablesInfo;

using Module.Core.LoginSystem.Commons.Framwork;
namespace Web
{
    public partial class WebForm2 : System.Web.UI.Page
    {

        private string userName = string.Empty;
        protected string noteRoot = string.Empty;
        protected string noteL = string.Empty;
        protected string noteR = string.Empty;
        protected string codeMemberL = string.Empty;
        protected string codeMemberR = string.Empty;
        private string idmember = "518";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                
                MemberNoteTreeInfo memberNoteTreeInfo = new MemberNoteTreeInfo();
                CHRTList<MemberNoteTreeInfo> list = new CHRTList<MemberNoteTreeInfo>();
                string codeMember = Request.QueryString["id"];
                if (codeMember == "" || codeMember == null)
                {
                    memberNoteTreeInfo = MemberNoteTreeBL.Select(2, idmember, "");
                    list = MemberNoteTreeBL.ListSelect(3, idmember, "");
                }
                else
                {
                    memberNoteTreeInfo = MemberNoteTreeBL.Select(2, codeMember, "");
                    list = MemberNoteTreeBL.ListSelect(3, codeMember, "");
                }
                if (memberNoteTreeInfo != null)
                {
                    noteRoot = memberNoteTreeInfo.FullName + " <br> ID: " + memberNoteTreeInfo.IdMember.ToString();
                }

                if (list != null && list.Count > 0)
                {
                    foreach (MemberNoteTreeInfo infoList in list)
                    {
                        if (infoList.Position == "L")
                        {
                            noteL = infoList.FullName + "<br> ID: " + infoList.IdMember.ToString();
                            codeMemberL = infoList.IdMember.ToString();
                        }
                        else if (infoList.Position == "R")
                        {
                            noteR = infoList.FullName + " <br> ID:" + infoList.IdMember.ToString();
                            codeMemberR = infoList.IdMember.ToString();
                        }

                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
        

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string codeparentMember = string.Empty;
            string postionRoot = string.Empty;
            string idMemberNote=string.Empty; // Idmember nút gốc cần hoán đổi
            DataTable dt = MemberNoteTreeBL.Select_Notte(6, txtIdmember.Text, ""); //xac dinh nut gốc can hoan doi
            if(dt !=null)
            {
                DataRow row = dt.Rows[dt.Rows.Count - 1];
                idMemberNote = row["IdMember"].ToString();
                codeparentMember = row["CodeMemberParent"].ToString();
                postionRoot = row["Position"].ToString();
                int count = MemberNoteTreeBL.CountStatus();
                if(count==0)
                {
                    postionRoot = txtPos.Text.Trim();
                }
            }           
            if (dt != null && dt.Rows.Count ==1)
            {
                MemberNoteTreeBL.Update(int.Parse(txtIdmember.Text), 1, int.Parse(txtParent.Text), 0, txtPos.Text, 1);
            }
            else if (dt != null && dt.Rows.Count >=2 )
            {                                
                aa(txtIdmember.Text.Trim(), idMemberNote, codeparentMember, postionRoot);
            }
            
        }                           
        void aa(string idMember, string idMemberNote, string codeparentMember, string postionRoot)
        {
            //đổi parent 2 nút E3 và A
            MemberNoteTreeBL.Update(int.Parse(idMemberNote), 0, int.Parse(txtParent.Text), 0,txtPos.Text, 1);//đổi A
            MemberNoteTreeBL.Update(int.Parse(idMember), 1, int.Parse(codeparentMember), 0, postionRoot, 1);//đổi E3

            MemberNoteTreeBL.Update(int.Parse(idMemberNote), 0, int.Parse(idMember), 1,"", 3);//update C,B
            MemberNoteTreeBL.Update(int.Parse(idMember), 0, int.Parse(idMemberNote), 1,"", 3);//update E4,E5
            MemberNoteTreeBL.Update(0, 0, 0, 0,"", 4);
        }

    }
}   